/*
 * @Author: w
 * @Date: 2022-02-19 22:38:30
 * @Description: 剑指 Offer 06. 从尾到头打印链表
 */

// 遍历链表

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {number[]}
 */

// 遍历
var reversePrint = function (head) {
  const ans = [];
  const pointer = head;

  while (pointer) {
    ans.unshift(pointer.val);
    pointer = pointer.next;
  }
  return ans;
};

// 递归
var reversePrint = function (head, arr = []) {
  return head == null ? [] : reversePrint(head.next).concat(head.val);
};
